package com.lagou.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.common.pojo.Goods;
import com.lagou.common.pojo.OrderDTO;
import com.lagou.common.pojo.Orders;
import com.lagou.order.fegin.GoodsFeign;
import com.lagou.order.mapper.OrderMapper;
import com.lagou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsFeign goodFeign;

    @Override
    public Orders queryById(Integer id) {
        //根据订单id查询出订单信息
        Orders order = orderMapper.queryById(id);
        //查询出该订单关联的商品id有哪些
        List<Integer> goodIds = orderMapper.getGoodIdsByOrderId(id);
        //根据商品id查询商品信息（远程调用商品微服务）
        List<Goods> goodList = goodFeign.queryByIds(goodIds);
        //把商品信息保存在对应的订单信息中
        order.setGoodList(goodList);
        return order;
    }

    @Override
    public Map<String, Object> queryByPage(OrderDTO orderDTO) {
        //开启分页
        PageHelper.startPage(orderDTO.getStartPage(),orderDTO.getPageSize());
        List<Orders> orders = orderMapper.queryByPage(orderDTO);
        PageInfo<Orders> info = new PageInfo<>(orders);
        //放入Map集合
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("list",orders);
        map.put("total", info.getTotal());
        return map;
    }
}
